<?php

/**
 * 商业逻辑类基类
 *
 * @author Alen
 * @since 1.0.0 2013-12-11 sutroon
 */
class SoBizBase
{

    /**
     * 数据库操作对象
     * @var SoDataModule 
     */
    public $dm;

    /**
     * 构造函数
     */
    public function __construct()
    {
        $this->dm = new SoDataModule();
    }

    /**
     * 析构函数
     */
    public function __destruct()
    {
        unset($this->dm);
    }

    /**
     * 通用删除数据库命令
     * @param type $id
     * @param type $search
     * @return string
     * @since 1.0.0 2013-12-14
     */
    public function delete_one($id, $search = 'ID=%d')
    {
        if ($id)
        {
            $this->dm->delete_records('', sprintf($search, $id));
            return $this->dm->has_error() ? $this->dm->get_error() : 'SUCCESS';
        }
        return 'Argument Mission!';
    }

    /**
     * 更新记录<br />
     * 成功则返回SUCCESS，否则返回错误消息
     * @param type $fields
     * @param type $search
     * @return type
     * @since 1.0.0 2013-12-17
     */
    public function update_records($fields, $search)
    {
        return $this->dm->update_records('', $fields, $search);
    }

    /**
     * 查询记录
     * @param type $search
     * @param type $fields
     * @return type
     * @since 1.0.0 2013-12-17
     */
    public function select_arrayset($search, $fields = '*')
    {
        return $this->dm->select_arrayset('', $search, $fields);
    }

    public function select_arraytreeset($search, $fields = 'ID, Name')
    {
        $dataset = $this->dm->select_arrayset('', $search, $fields);
        if (!$dataset)
            return $dataset;
        $ds = null;
        $r = NULL;
        $count = count($dataset);
        $i = 0;
        while ($i < $count)
        {
            $this->select_arraytreeset_func($dataset, $dataset[$i]['ID'], $fields);
            $i++;
        }
        return $dataset;
    }

    private function select_arraytreeset_func(&$dataset, $fid, $fields)
    {
        $count = count($dataset);
        $i = 0;
        $ds;
        while ($i < $count)
        {
            $row = &$dataset[$i];
            $ds = $this->dm->select_arrayset('', 'ParentID=' . $fid, $fields);
            $row['children'] = $ds;
            if (isset($row['childdren']))
                $this->select_arraytreeset_func($row['childdren'], $row['ID'], $fields);
            $i++;
        }
    }

    /**
     * 查询字段的值
     * @param type $search
     * @param type $field
     * @return type
     */
    public function select_value($search, $field)
    {
        return $this->dm->select_value('', $search, $field);
    }

    /*     * ***************************************************************************************
     * 通用方法(GM Begin)
     * 
     * 与数据库无关的通用方法
     * **************************************************************************************** */

    /**
     * 获取状态名称
     * @param type $params
     * @return type
     * @example switch_state(array('v'=>1,'group'=>array('1'=>'正常'),'defv'=>''));
     * @since 1.0.0 2013-10-3 sutroon
     */
    function switch_state($params)
    {
        extract($params);
        return array_key_exists($v, $group) ? $group[strval($v)] : ($defv ? $defv : $v);
    }

    /**
     * 获取状态数组
     * @param type $modul
     * @return type
     * @since 1.0.0 2014-3-27 by sutroon
     */
    public function get_state_array($modul = '')
    {
        switch (strtolower($modul))
        {
            case 'order':
                return array(
                    '0' => '未付款',
                    '100' => '货到付款未发货',
                    '110' => '已付款未发货',
                    '200' => '已发货',
                    '300' => '已签收',
                    '400' => '买家取消订单',
                    '410' => '卖家取消订单',
                    '500' => '交易完成'
                );
            case 'recharge':
            case 'withdrawal':
                return array(
                    '0' => '未审核',
                    '1' => '审核通过',
                    '4' => '审核未通过'
                );
            default:
                return array(
                    '0' => '关闭',
                    '1' => '正常'
                );
        }
    }

    function get_state_name($state, $modul = '')
    {
        $arr = $this->get_state_array($module);
        if (array_key_exists($state, $arr))
        {
            return $arr[$state];
        }
        return '状态异常';
    }

    /**
     * 获取类型数组
     * @param type $module
     * @return type
     * @since 1.0.0 2014-3-27 by sutroon
     */
    public function get_type_array($module = 'finance')
    {
        return array(
            'OUT_TRANSACTION' => '购物支付',
            'OUT_AA' => 'AA付款',
            'OUT_TRANSFER' => '转账支出',
            'OUT_WITHDRAWAL' => '提现扣款',
            'OUT_RENEWAL' => '续费扣款',
            'OUT_PREORDER_SEAT' => '预购席位扣款',
            'OUT_WINNING' => '中奖奖励支出',
            'IN_TRANSACTION' => '交易收入',
            'IN_AA' => 'AA收款',
            'IN_TRANSFER' => '转账收入',
            'IN_RECHARGE' => '充值收款',
            'IN_RENEWAL' => '续费收款',
            'IN_PREORDER_SEAT' => '预购席位收款',
            'IN_COMMISSION' => '提成收入',
            'IN_WINNING' => '中奖奖励收入'
        );
    }

    function get_type_name($type, $module = '')
    {
        $arr = $this->get_type_array($module);
        if (array_key_exists($type, $arr))
        {
            return $arr[$type];
        }
        return '未知类型';
    }

    /*     * *********************************************************************通用方法(GM End)### */
}
